﻿namespace DotRas
{
    using System;
    using System.Runtime.InteropServices;
    using System.Security.Permissions;
    using System.Text;

    [Serializable, StructLayout(LayoutKind.Sequential)]
    public struct Luid : IEquatable<Luid>, IFormattable
    {
        public static readonly Luid Empty;
        private int _lowPart;
        private int _highPart;
        public Luid(int lowPart, int highPart)
        {
            this._lowPart = lowPart;
            this._highPart = highPart;
        }

        [SecurityPermission(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)]
        public static Luid NewLuid()
        {
            Luid empty = Empty;
            int cb = Marshal.SizeOf(typeof(Luid));
            IntPtr zero = IntPtr.Zero;
            try
            {
                zero = Marshal.AllocHGlobal(cb);
                if (SafeNativeMethods.AllocateLocallyUniqueId(zero))
                {
                    return (Luid) Marshal.PtrToStructure(zero, typeof(Luid));
                }
                DotRas.ThrowHelper.ThrowWin32Exception();
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(zero);
                }
            }
            return empty;
        }

        public static bool operator ==(Luid objA, Luid objB)
        {
            return objA.Equals(objB);
        }

        public static bool operator !=(Luid objA, Luid objB)
        {
            return !objA.Equals(objB);
        }

        public override bool Equals(object obj)
        {
            bool flag = false;
            if (obj is Luid)
            {
                flag = this.Equals((Luid) obj);
            }
            return flag;
        }

        public bool Equals(Luid other)
        {
            return ((this._highPart == other._highPart) && (this._lowPart == other._lowPart));
        }

        public override int GetHashCode()
        {
            long num = this._highPart + this._lowPart;
            if (num < -2147483648L)
            {
                return -2147483648;
            }
            if (num > 0x7fffffffL)
            {
                return 0x7fffffff;
            }
            if ((num >= -2147483648L) && (num <= 0x7fffffffL))
            {
                return (int) num;
            }
            return 0;
        }

        public override string ToString()
        {
            return this.ToString(null, null);
        }

        public string ToString(string format)
        {
            return this.ToString(format, null);
        }

        public string ToString(string format, IFormatProvider formatProvider)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(this._lowPart.ToString(format, formatProvider)).Append("-").Append(this._highPart.ToString(format, formatProvider));
            return builder.ToString();
        }

        static Luid()
        {
            Empty = new Luid();
        }
    }
}

